/**
 * 高德组件依赖，可能还包括高德api的特性扩展
 */
import { amapKey } from '../configs/config';
import { randomStr } from './util';

const key = randomStr();
const mapApiUrl = `http://webapi.amap.com/maps?v=1.3&key=${amapKey}&callback=${key}`;

let scriptOnload = false;

const onloadFun = [];
const script = document.createElement('script');
script.src = mapApiUrl;
script.async = 'async';
window[key] = () => {
	scriptOnload = true;
	onloadFun.forEach(fun => fun());
};
document.body.appendChild(script);

export default function mapOnLoad(callback) {
	if (scriptOnload) callback();
	else onloadFun.push(callback);
}

export function isMapOnLoad() {
	return scriptOnload;
}
